<?php
class ModelAccountSign extends Model {
	public function addSign($data) {

	     $this->db->query("INSERT INTO " . DB_PREFIX . "sign SET customer_id = '" . (int)$this->customer->getId()  . "', num = '" . (int)$data['num'] . "', last_date = now() ");
	}

	public function addSignRecord(){
	    $this->db->query("INSERT INTO " . DB_PREFIX . "sign_record SET customer_id = '" . (int)$this->customer->getId() . "', date_added = now()");
	}


	public function updateSign($data){
	    $this->db->query("UPDATE " . DB_PREFIX . "sign SET  num = '" . (int)$data['num'] . "', last_date = now() where customer_id = '" . (int)$this->customer->getId() . "'");
	}

	//每个用户一条记录
	public function getSign($start_time,$end_time){
	    if(isset($start_time)&&isset($end_time)){
	        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "sign WHERE customer_id = '" . (int)$this->customer->getId() . "' and last_date >= '".$start_time."' and last_date <='".$end_time."'");
	    }else{
	        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "sign WHERE customer_id = '" . (int)$this->customer->getId() . "'");
	    }
	    return $query->row;
	}

	public function getSignRecords($start_time,$end_time){
	    $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "sign_record WHERE customer_id = '" . (int)$this->customer->getId() . "'and date_added >= '".$start_time."' and date_added <='".$end_time."'");

	    return $query->rows;
	}


	public function getTodaySignRank($start_time,$end_time){


        $query = $this->db->query("SELECT count(*) rank FROM " . DB_PREFIX . "sign_record a WHERE a.date_added <= (SELECT  b.date_added FROM " . DB_PREFIX . " sign_record b where b.customer_id = '".(int)$this->customer->getId()."' and b.date_added >= '".$start_time."' and b.date_added <='".$end_time."')  and a.date_added >= '".$start_time."' and a.date_added <='".$end_time."'");

        return $query->row['rank'];
    }
}